package com.statonline.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.statonline.domain.Persona;
import com.statonline.service.PersonaManager;
 
@Controller
public class LoginController {
 
	@Autowired
	private PersonaManager contactManager;
	@Autowired
	private UserDetailsManager userDetailsManager;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private SaltSource saltSource;
	
	@RequestMapping(value="/welcome.htm", method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String name = user.getUsername();
		model.addAttribute("username", name);
		model.addAttribute("message", "Spring Security login + database example");
		return "hello";
	}
 
	@RequestMapping(value="/login.htm", method = RequestMethod.GET)
	public String login(ModelMap model) {
		return "login";
	}
	
	@RequestMapping(value="/adduser.htm", method=RequestMethod.GET)
	public String startAddUser(Model model) {
	/*	public String startAddUser(@RequestParam("user")String username,
			@RequestParam("password") String password,Model model) {
		
		
	   List<GrantedAuthority> authorites = new ArrayList<GrantedAuthority>();
	   authorites.add(new GrantedAuthorityImpl("ROLE_USER"));

	   User user = new User(username, passwordEncoder.encodePassword(password,null),
	           true, false, false, false, authorites);
	   
	   User saltedUser = new User(username, passwordEncoder.encodePassword(password,
		        saltSource.getSalt(user)), true, false, false, false, authorites);

		userDetailsManager.createUser(saltedUser);
		*/
	   return "addContact";
	}
	
	@RequestMapping(value="/adduser.htm", method=RequestMethod.POST)
	public String createUser(@RequestParam("firstname")String firstname,
			@RequestParam("lastname")String lastname,
			@RequestParam("email")String email,
			@RequestParam("user")String username,
			@RequestParam("password") String password,
			Model model) {
		
		
		/*
		 * Creacion del usuario para la persona
		 */

	   List<GrantedAuthority> authorites = new ArrayList<GrantedAuthority>();
	   authorites.add(new GrantedAuthorityImpl("ROLE_USER"));

	   User user = new User(username, passwordEncoder.encodePassword(password,null),
	           true, false, false, false, authorites);
	   
	   User saltedUser = new User(username, passwordEncoder.encodePassword(password,
		        saltSource.getSalt(user)), true, false, false, false, authorites);

		userDetailsManager.createUser(saltedUser);
		
		/*
		 * Contacto para agregar en la tabla Persona
		 */
		Persona newContact = new Persona();
		
		newContact.setFirstname(firstname);
		newContact.setLastname(lastname);
		newContact.setEmail(email);
		newContact.setUsername(username);
		
		contactManager.addContact(newContact);

	   return "login";
	}
	
	
	@RequestMapping(value="/loginfailed.htm", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
 
		model.addAttribute("error", "true");
		return "login";
 
	}
	
	@RequestMapping(value="/logout.htm", method = RequestMethod.GET)
	public String logout(ModelMap model) {
		return "login";
	}
	
	/*
	@RequestMapping(value="/loginAdd", method = RequestMethod.GET)
	public String logAdd(ModelMap model) {
		return "redirect:usercontroller_addStart";
	}
	*/
}